#include "stdio.h"
#include "string.h"
void itoa(unsigned int i,unsigned char *s,int o)
{
	int tmp;
	int k=0,k2;
	char *p, ss[100];
	p = ss;
	while(1)
	{
		//printf("\r\n%s,%d\r\n",__FUNCTION__,i);
		tmp=i%2;
		k++;
		if(tmp)
			*p = '1';
		else
			*p = '0';
		p++;
		i=i/2;
		if(i == 0)
			break;
	}
	*(s+k) = 0 ;
	k2=k;
	for(k;k>0;k--)
	{
		*(s+k-1)=ss[k2-k];
	}
}

typedef struct 
{
	unsigned int sync:14;               //11
	unsigned int version:2;            //2
	unsigned int layer:2;              //2
	unsigned int error_protection:1;   //1
	unsigned int bitrate_index:4;      //4
	unsigned int sampling_frequency:2; //2
	unsigned int padding:1;            //1
	unsigned int private:1;            //1
	unsigned int mode:1;               //2
	unsigned int mode_extension:2;     //2
	unsigned int copyright:2;          //1
	unsigned int original:1;           //1
	unsigned int emphasis:2;           //2
}HEADER,*LPHEADER;

typedef union UHeaher
{
	unsigned char cheader[4];
	unsigned int iheader;
}UHEADER,*LPUHEADER;


HEADER header;
//LPHEADER lpheader = &header;

UHEADER uheader;
//LPHEADER lpuheader=&uheader;

unsigned char ccheader[4];
unsigned int  iheader;

int main()
{
	char s[10][100];
	unsigned char tmp;
	LPHEADER lpheader = &header;
	LPUHEADER lpuheader = &uheader;
	FILE *fp;
	lpuheader->iheader=0x12345678;
	fp=fopen("test.mp3","rb");
	if(NULL != fp)
	{
		int flag=0;
		if(lpuheader->cheader[1] == 0x12)
		{
			flag = 0;
			printf("\r\nbig end\rn");
		}
		else
		{
			flag = 0;
			printf("\r\nlittle end\r\n");
		}
		
		printf("%d",sizeof(header));
		//fread(&tmp,1,1,fp);
		printf("\r\ntmp=%02x",tmp);
		fread(&ccheader[0],sizeof(ccheader[0]),sizeof(ccheader),fp);
		//ifread(&cheader[0],sizeof(cheader[0]),1,fp);
		printf("\r\n%d,%d\r\n",sizeof(ccheader[0]),sizeof(ccheader));
		printf("\r\n%02x,%02x,%02x,%02x",ccheader[0],ccheader[1],ccheader[2],ccheader[3]);
		itoa(ccheader[0],s[0],2);
		itoa(ccheader[1],s[1],2);
		itoa(ccheader[2],s[2],2);
		itoa(ccheader[3],s[3],2);
		printf("\r\nb=%s,%s,%s,%s\r\n",s[0],s[1],s[2],s[3]);
		if( flag == 1 )
		{
			tmp        = ccheader[3];
			ccheader[3] = ccheader[0];
			ccheader[0] = tmp;
			tmp        = ccheader[2];
			ccheader[2] = ccheader[1];
			ccheader[1] = tmp;

			//tmp        = cheader[0];
			//cheader[0] = cheader[1];
			//cheader[1] = tmp;
			
		}
		printf("\r\n%02x,%02x,%02x,%02x",ccheader[0],ccheader[1],ccheader[2],ccheader[3]);
		//lpheader = (LPHEADER)ccheader;
	        //	memcpy((unsigned char *)lpheader,ccheader,4);
		itoa(lpheader->sync,s[0],2);
		itoa(lpheader->version,s[1],2);
		itoa(lpheader->layer,s[2],2);
		itoa(lpheader->sampling_frequency,s[3],2);
		itoa(*((unsigned int * )lpheader) ,s[4],2);
		printf("\r\nb=%s,%s,%s,%s,%s\r\n",s[0],s[1],s[2],s[3],s[4]);
		lpheader->sync               =  ccheader[0];
		lpheader->sync 		     = (lpheader->sync << 3)|( ccheader[1] >> 5);
		lpheader->version            = (ccheader[1] & 0x18) >> 3;
		lpheader->layer              = (ccheader[1] & 0x06) >> 1;
		lpheader->error_protection   =  ccheader[1] & 0x01;
		lpheader-> bitrate_index     = (ccheader[2] & 0xf0) >> 4;
		lpheader->sampling_frequency = (ccheader[2] & 0x06) >> 2;
		printf("\r\n%d,%d\r\n",(ccheader[1] & 0x01) << 3,((ccheader[2] & 0xe0) >> 5));		
		
		itoa(*((unsigned int * )lpheader) ,s[4],2);
                printf("\r\nb=%s,%s,%s,%s,%s\r\n",s[0],s[1],s[2],s[3],s[4]);
		printf("\r\n%d,%d,%d,%d,%d,%d\r\n",lpheader->sync,lpheader->version,lpheader->layer,lpheader->error_protection,lpheader-> bitrate_index,lpheader->sampling_frequency);
		fclose(fp);
	}
	else
	{
		printf("\r\nopen err\r\n");
	}

}
